package com.gomtel.smartdevice.widget;

import android.os.Handler;

import com.gomtel.smartdevice.bean.UserContent;
import com.gomtel.smartdevice.moudle.DataManager;
import com.gomtel.smartdevice.moudle.db.entity.LoginUserInfo;
import com.gomtel.smartdevice.utils.LogUtil;

import java.util.List;

import cn.jiguang.share.android.api.AuthListener;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.model.AccessTokenInfo;
import cn.jiguang.share.android.model.BaseResponseInfo;
import cn.jiguang.share.android.model.UserInfo;
import cn.jiguang.share.android.utils.Logger;

/**
 * Created by Administrator on 2018/5/31 0031.
 */

public class LoginWidgetClass {

    private List<String> platformList;
    private String TAG = "LoginWidgetClass";
    private DataManager dataManager;
    public static String contentInfo = "";
    private Handler handler;

    public LoginWidgetClass(DataManager dataManager, Handler handler) {
        this.dataManager = dataManager;
        this.handler = handler;
    }

    public void RemovePermissions() {
        initPermission(contentInfo);
    }

    public void initPermission(String permissionType) {
        contentInfo = permissionType;
        if (!JShareInterface.isAuthorize(permissionType)) {
            JShareInterface.authorize(permissionType, mAuthListener);
        } else {
            JShareInterface.removeAuthorize(permissionType, mAuthListener);
        }
    }

    public void initUserInfo() {
        JShareInterface.getUserInfo(contentInfo, mAuthListener);
    }

    /**
     * 授权、获取个人信息回调
     * action ：Platform.ACTION_AUTHORIZING 授权
     * Platform.ACTION_USER_INFO 获取个人信息
     */
    private AuthListener mAuthListener = new AuthListener() {
        @Override
        public void onComplete(Platform platform, int action, BaseResponseInfo data) {
            String toastMsg = null;
            switch (action) {
                case Platform.ACTION_AUTHORIZING:
                    if (data instanceof AccessTokenInfo) {        //授权信息
                        String token = ((AccessTokenInfo) data).getToken();//token
                        long expiration = ((AccessTokenInfo) data).getExpiresIn();//token有效时间，时间戳
                        String refresh_token = ((AccessTokenInfo) data).getRefeshToken();//refresh_token
                        String openid = ((AccessTokenInfo) data).getOpenid();//openid
                        //授权原始数据，开发者可自行处理
                        String originData = data.getOriginData();
                        toastMsg = "授权成功:" + data.toString();
                        Logger.dd(TAG, "openid:" + openid + ",token:" + token + ",expiration:" + expiration + ",refresh_token:" + refresh_token);
                        Logger.dd(TAG, "originData:" + originData);
                        handler.sendEmptyMessage(0);
                    }
                    break;
                case Platform.ACTION_REMOVE_AUTHORIZING:
                    toastMsg = "删除授权成功";
                    break;
                case Platform.ACTION_USER_INFO:
                    String sex = "男";
                    if (data instanceof UserInfo) {      //第三方个人信息
                        String openid = ((UserInfo) data).getOpenid();  //openid
                        String name = ((UserInfo) data).getName();  //昵称
                        String imageUrl = ((UserInfo) data).getImageUrl();  //头像url
                        int gender = ((UserInfo) data).getGender();//性别, 1表示男性；2表示女性
                        //个人信息原始数据，开发者可自行处理
                        String originData = data.getOriginData();
                        toastMsg = "获取个人信息成功:" + data.toString();
                        Logger.dd(TAG, "openid:" + openid + ",name:" + name + ",gender:" + gender + ",imageUrl:" + imageUrl);
                        Logger.dd(TAG, "originData:" + originData);
                        if (gender != 1)
                            sex = "女";
                        insertUserInfo("admin", "admin123", name, imageUrl, sex);
                    }
                    handler.sendEmptyMessage(1);
                    break;
            }
            LogUtil.e(TAG, "toastMsg : " + toastMsg);
        }

        @Override
        public void onError(Platform platform, int action, int errorCode, Throwable error) {
            Logger.dd(TAG, "onError:" + platform + ",action:" + action + ",error:" + error);
            String toastMsg = null;
            switch (action) {
                case Platform.ACTION_AUTHORIZING:
                    toastMsg = "授权失败";
                    break;
                case Platform.ACTION_REMOVE_AUTHORIZING:
                    toastMsg = "删除授权失败";
                    break;
                case Platform.ACTION_USER_INFO:
                    toastMsg = "获取个人信息失败";
                    handler.sendEmptyMessage(2);
                    break;
            }
            LogUtil.e(TAG, "toastMsg : " + toastMsg);
        }

        @Override
        public void onCancel(Platform platform, int action) {
            Logger.dd(TAG, "onCancel:" + platform + ",action:" + action);
            String toastMsg = null;
            switch (action) {
                case Platform.ACTION_AUTHORIZING:
                    toastMsg = "取消授权";
                    break;
                // TODO: 2017/6/23 删除授权不存在取消
                case Platform.ACTION_REMOVE_AUTHORIZING:
                    break;
                case Platform.ACTION_USER_INFO:
                    toastMsg = "取消获取个人信息";
                    break;
            }
            LogUtil.e(TAG, "toastMsg : " + toastMsg);
        }
    };

    public void insertUserInfo(String username, String password, String nickName, String userPhoto, String sex) {
        UserContent.UserName = username;
        LoginUserInfo loginUserInfo = dataManager.queryLoginUserInfo(username);
        if (loginUserInfo != null) {
            dataManager.UpDataLoginUserInfo(new LoginUserInfo(loginUserInfo.getId(), username, password, nickName, userPhoto, sex));
        } else {
            dataManager.insertLoginUserInfo(new LoginUserInfo(username, password, nickName, userPhoto, sex));
        }
    }


}
